package com.lockmem.Conf;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class ConfHelper {
	private String tag = "ConfHelper";
	private SharedPreferences conf = null;
	private Context context;
	private boolean NeedUpdateSharedPref = false; //当初始化数据有更新时置为true便于更新
	public final static String SharePrefName = "SharePre";

	private SharedPreferences getSharePref() {
		if (conf == null)
			conf = context.getSharedPreferences(SharePrefName,
					Context.MODE_PRIVATE);
		return conf;
	}

	public ConfHelper(Context ser) {
		this.context = ser;
	}

	/**
	 * @param ser
	 * @return true表示创建，false表示已存在，无需创建
	 */
	private boolean createConf() {
		//Can't exchange the order besides the "||"
		if(getSharePref().getInt(ConfKey.Count, -1)==-1||NeedUpdateSharedPref)
		{
			Editor editor = conf.edit();//获取编辑器
			editor.putInt(ConfKey.Count, 3);
			editor.putInt(ConfKey.Current_Resource, ConfValue.studyresource_english);
			editor.putFloat(ConfKey.Current_Version, ConfValue.Current_Version);
			editor.putBoolean(ConfKey.Switch, ConfValue
					.switch_on);
			editor.putInt(ConfKey.ModeTime, 1218);
			editor.putBoolean(ConfKey.Mode_ChEn, ConfValue.CE_EtoC);
			editor.putInt(ConfKey.Order, ConfValue.order_normal);
			editor.putInt(ConfKey.Skin, ConfValue.Skin_Pink);
			editor.commit();//提交修改
			return true;
		}
		else 
			return false ;
	}
	
	public SharedPreferences getConf() {
		if (conf == null) {
			createConf();
		}
		return conf;
	}

	public boolean getSwitchState() {
		return getConf().getBoolean(ConfKey.Switch, true);
	}

	public void put(String key, int value) {
		// TODO Auto-generated method stub
		Editor editor = getConf().edit();
		editor.putInt(key, value);
		editor.commit();
	}
	public void put(String key, float value) {
		// TODO Auto-generated method stub
		Editor editor = getConf().edit();
		editor.putFloat(key, value);
		editor.commit();
	}
	public void put(String key, boolean value) {
		// TODO Auto-generated method stub
		Editor editor = getConf().edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public int getCurrentResource() {
		// TODO Auto-generated method stub
		return getConf().getInt(ConfKey.Current_Resource, -1);
	}

	public float getCurrentVersion() {
		// TODO Auto-generated method stub
		return getConf().getFloat(ConfKey.Current_Version, -1);
	}

	public int getOrder() {
		// TODO Auto-generated method stub
		return getConf().getInt(ConfKey.Order, -1);
	}

	public int getSkin() {
		// TODO Auto-generated method stub
		return getConf().getInt(ConfKey.Skin,-1);
	}
}
